﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace pGrid
{
  public partial class MainWindow : Window
  {
    private Game newGame = new Game();
    private RemoveStations removeDialog;


    public MainWindow()
    {
      newGame = this.TryFindResource("game_key") as Game;
      newGame.SetGameParams(3); 
      InitializeComponent();
      InitResourceManager();
      ratingGrid.SelectedIndex = 0;
      DisplayExpected();



      
    }

    void InitResourceManager()
    {
     Button[] c_list = new Button[24];
     Button[] o_list = new Button[24];
     Button[] w_list = new Button[24];
     Button[] u_list = new Button[24];
     c_list[0] = coal1_1; c_list[1] = coal1_2; c_list[2] = coal1_3; c_list[3] = coal2_1; c_list[4] = coal2_2; c_list[5] = coal2_3;
     c_list[6] = coal3_1; c_list[7] = coal3_2; c_list[8] = coal3_3; c_list[9] = coal4_1; c_list[10] = coal4_2; c_list[11] = coal4_3;
     c_list[12] = coal5_1; c_list[13] = coal5_2; c_list[14] = coal5_3; c_list[15] = coal6_1; c_list[16] = coal6_2; c_list[17] = coal6_3;
     c_list[18] = coal7_1; c_list[19] = coal7_2; c_list[20] = coal7_3; c_list[21] = coal8_1; c_list[22] = coal8_2; c_list[23] = coal8_3;

     o_list[0] = oil1_1; o_list[1] = oil1_2; o_list[2] = oil1_3; o_list[3] = oil2_1; o_list[4] = oil2_2; o_list[5] = oil2_3;
     o_list[6] = oil3_1; o_list[7] = oil3_2; o_list[8] = oil3_3; o_list[9] = oil4_1; o_list[10] = oil4_2; o_list[11] = oil4_3;
     o_list[12] = oil5_1; o_list[13] = oil5_2; o_list[14] = oil5_3; o_list[15] = oil6_1; o_list[16] = oil6_2; o_list[17] = oil6_3;
     o_list[18] = oil7_1; o_list[19] = oil7_2; o_list[20] = oil7_3; o_list[21] = oil8_1; o_list[22] = oil8_2; o_list[23] = oil8_3;

     w_list[0] = waste1_1; w_list[1] = waste1_2; w_list[2] = waste1_3; w_list[3] = waste2_1; w_list[4] = waste2_2; w_list[5] = waste2_3;
     w_list[6] = waste3_1; w_list[7] = waste3_2; w_list[8] = waste3_3; w_list[9] = waste4_1; w_list[10] = waste4_2; w_list[11] = waste4_3;
     w_list[12] = waste5_1; w_list[13] = waste5_2; w_list[14] = waste5_3; w_list[15] = waste6_1; w_list[16] = waste6_2; w_list[17] = waste6_3;
     w_list[18] = waste7_1; w_list[19] = waste7_2; w_list[20] = waste7_3; w_list[21] = waste8_1; w_list[22] = waste8_2; w_list[23] = waste8_3;

     u_list[0] = uranium1; u_list[1] = uranium2; u_list[2] = uranium3; u_list[3] = uranium4; u_list[4] = uranium5; u_list[5] = uranium6;
     u_list[6] = uranium7; u_list[7] = uranium8; u_list[8] = uranium10; u_list[9] = uranium12; u_list[10] = uranium14; u_list[11] = uranium16;

     newGame.ResManager.SetButtons(c_list, o_list, w_list, u_list);
     newGame.ResManager.DisplayResources();
    }

    //-------------------------LOGIC FUNCTIONS---------------------------------------------------------------------

    public void Auction()
    {
      this.auction_tabItem.Visibility = Visibility.Visible;
      this.auction_tabItem.IsSelected = true;
      InitAuction();
    
      MessageBoxResult result = MessageBoxResult.No;
      while (result != MessageBoxResult.Yes)
      {

        newGame.AuctionManager.CurrentPlayer = newGame.GetByRating(newGame.AuctionManager.Turn);
        newGame.AuctionManager.HighestBetPlayer = newGame.GetByRating(newGame.AuctionManager.Turn);
        result = MessageBox.Show("Buy power station?", newGame.AuctionManager.CurrentPlayer.Name, MessageBoxButton.YesNo);
        if (result == MessageBoxResult.Yes)
        {
          newGame.AuctionManager.CurrentPlayer.StartedAuction = true;
          newGame.AuctionManager.Init();
          foreach (Player p in newGame.PlayerList) // + если бабла нет
          {
            p.CanBet = p.BuyStation;
            if (p.CanBet)
              newGame.AuctionManager.PlayersInAuction++;
          }
          return;
        }
        else // Не хочет покупать
        {
          (newGame.GetByRating(newGame.AuctionManager.Turn++)).BuyStation = false; // больше не покупает в этот раунд и переходит ход к следующему
          if (newGame.AuctionManager.Turn == newGame.PlayerList.Count) // закончился этап покупки электростанций !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
             return;
          //newGame.AuctionManager.Pos++;
          this.button1.Content = "Next";
        }
      }
      if (newGame.AuctionManager.Pos >= newGame.PlayerList.Count)
      {
        this.auction_tabItem.Visibility = Visibility.Hidden;
        this.auction_tabItem.IsSelected = false;
        this.button1.Content = "";
      }
      
    }
    
    void BuyCoal(int price)
    {
      newGame.BuyCoal(price);
      this.ratingGrid.Items.Refresh();
      newGame.ResManager.EnableResourcesForPlayer(newGame.CurrentPlayer);
    }

    void BuyOil(int price)
    {
      newGame.BuyOil(price);
      this.ratingGrid.Items.Refresh();
      newGame.ResManager.EnableResourcesForPlayer(newGame.CurrentPlayer);
    }

    void BuyWaste(int price)
    {
      newGame.BuyWaste(price);
      this.ratingGrid.Items.Refresh();
      newGame.ResManager.EnableResourcesForPlayer(newGame.CurrentPlayer);
    }

    void BuyUranium(int price)
    {
      newGame.BuyUranium(price);
      this.ratingGrid.Items.Refresh();
      newGame.ResManager.EnableResourcesForPlayer(newGame.CurrentPlayer);
    }

    void BuyStation(int price)
    {
      if (((newGame.CurrentPlayer.BuyFourthStation) && (newGame.CurrentPlayer.StationsNum == 4)) ||     // если у игрока станций больше, чем разрешено иметь
          ((!newGame.CurrentPlayer.BuyFourthStation) && (newGame.CurrentPlayer.StationsNum == 3)))
        DisplayRemoveStation(price);                                                                        // предлагается выбрать, какую станцию нужно заменить
      else                                                                                              // если еще можно купить станцию,
        newGame.BuyStation(price);                                                                          // просто покупается новая

      DisplayExpected();
      ratingGrid.Items.Refresh();
      DisplayCurrentProfile(newGame.CurrentPlayer);
    }
    //-------------------------DISPLAY SMTH---------------------------------------------------------------------
    void DisplayRemoveStation(int num_bought_station)
    {
      removeDialog = new RemoveStations();
      removeDialog.SetStations(newGame.CurrentPlayer);
      removeDialog.BoughtNum = num_bought_station;
      removeDialog.Owner = this;
      removeDialog.Closing += new System.ComponentModel.CancelEventHandler(removeDialog_Closing);
      removeDialog.ShowDialog();
      //removeDialog.IsActive = true;
    }
    void DisplayExpected()
    {
      this.current_station1.Source = newGame.StationManager.StationsExpected.ElementAt(0).Value.Pic;
      this.current_station2.Source = newGame.StationManager.StationsExpected.ElementAt(1).Value.Pic;
      this.current_station3.Source = newGame.StationManager.StationsExpected.ElementAt(2).Value.Pic;
      this.current_station4.Source = newGame.StationManager.StationsExpected.ElementAt(3).Value.Pic;
      this.future_station1.Source = newGame.StationManager.StationsExpected.ElementAt(4).Value.Pic;
      this.future_station2.Source = newGame.StationManager.StationsExpected.ElementAt(5).Value.Pic;
      this.future_station3.Source = newGame.StationManager.StationsExpected.ElementAt(6).Value.Pic;
      this.future_station4.Source = newGame.StationManager.StationsExpected.ElementAt(7).Value.Pic;
    }
    void DisplayCurrentProfile(Player selectedPlayer)
    {
      if (selectedPlayer.Station1 != null)
      {
        this.player_station1.Source = selectedPlayer.Station1.Pic;
        this.station_res_1.Text = selectedPlayer.Station1.RealResNum.ToString();
      }
      else
      {
        this.player_station1.Source = newGame.SocketPic;
        this.station_res_1.Text = "";
      }

      if (selectedPlayer.Station2 != null)
      {
        this.player_station2.Source = selectedPlayer.Station2.Pic;
        this.station_res_2.Text = selectedPlayer.Station2.RealResNum.ToString();
      }
      else
      {
        this.player_station2.Source = newGame.SocketPic;
        this.station_res_2.Text = "";
      }

      if (selectedPlayer.Station3 != null)
      {
        this.player_station3.Source = selectedPlayer.Station3.Pic;
        this.station_res_3.Text = selectedPlayer.Station3.RealResNum.ToString();
      }
      else
      {
        this.player_station3.Source = newGame.SocketPic;
        this.station_res_3.Text = "";
      }
      if (selectedPlayer.BuyFourthStation)
      {
        if (selectedPlayer.Station4 != null)
        {
          this.player_station4.Source = selectedPlayer.Station4.Pic;
          this.station_res_4.Text = selectedPlayer.Station4.RealResNum.ToString();
        }
        else
        {
          this.player_station4.Source = newGame.SocketPic;
          this.station_res_4.Text = "";
        }
      }
    }
    void DisplayAuction()
    {
      this.auction_station.Source = newGame.CurrentStation.Pic;
      this.highest_bet_label2.Content = newGame.AuctionManager.CurrentPlayer.Name;
      this.highest_bet_label4.Content = newGame.CurrentStation.Cost.ToString(); // current price
      this.newGame.AuctionManager.HighestBet = newGame.CurrentStation.Cost;
      this.your_bet_label2.Content = newGame.AuctionManager.CurrentPlayer.Name;
      this.your_bet_textbox.Text = (newGame.CurrentStation.Cost + 1).ToString();
      //this.bet_button.IsEnabled = true; //игрок, покупающий станцию в этот ход, не может пасовать сразу после выбора станции, может только повысить цену 
    }
    void InitAuction()
    {
      this.auction_station.Source = newGame.SocketPic;
      this.highest_bet_label2.Content = "...";
      this.highest_bet_label4.Content = "0"; // current price
      this.your_bet_label2.Content ="...";
      this.your_bet_textbox.Text = "0";
      this.bet_button.IsEnabled = false;
      this.fold_button.IsEnabled = false;
    }

    void UpdateAuction()
    {
      this.highest_bet_label2.Content = newGame.AuctionManager.HighestBetPlayer.Name;
      this.highest_bet_label4.Content = newGame.AuctionManager.HighestBet.ToString();
      this.your_bet_textbox.Text = (newGame.AuctionManager.HighestBet + 1).ToString();    
    }
    //-------------------------HANDLERS-------------------------------------------------------------------------

    public void waitForClick(object sender, RoutedEventArgs e)
    {
    
    }
    private void ratingGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (ratingGrid.SelectedIndex == -1) return;
      Player selectedPlayer  = (Player)ratingGrid.SelectedItem;
      newGame.ResManager.EnableResourcesForPlayer(selectedPlayer);
      DisplayCurrentProfile(selectedPlayer);

    }

    #region  resourse_handlers
    
    private void coal1_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal1_1.Visibility = Visibility.Hidden;
      BuyCoal(1);
    }

    private void coal1_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal1_2.Visibility = Visibility.Hidden;
      BuyCoal(1);
    }

    private void coal1_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal1_3.Visibility = Visibility.Hidden;
      BuyCoal(1);
    }

    private void oil1_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil1_1.Visibility = Visibility.Hidden;
      BuyOil(1);
    }

    private void oil1_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil1_2.Visibility = Visibility.Hidden;
      BuyOil(1);
    }

    private void oil1_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil1_3.Visibility = Visibility.Hidden;
      BuyOil(1);
    }

    private void waste1_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste1_1.Visibility = Visibility.Hidden;
      BuyWaste(1);
    }

    private void waste1_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste1_2.Visibility = Visibility.Hidden;
      BuyWaste(1);
    }

    private void waste1_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste1_3.Visibility = Visibility.Hidden;
      BuyWaste(1);
    }

    private void coal2_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal2_1.Visibility = Visibility.Hidden;
      BuyCoal(2);
    }

    private void coal2_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal2_2.Visibility = Visibility.Hidden;
      BuyCoal(2);
    }

    private void coal2_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal2_3.Visibility = Visibility.Hidden;
      BuyCoal(2);
    }

    private void oil2_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil2_1.Visibility = Visibility.Hidden;
      BuyOil(2);
    }

    private void oil2_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil2_2.Visibility = Visibility.Hidden;
      BuyOil(2);
    }

    private void oil2_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil2_3.Visibility = Visibility.Hidden;
      BuyOil(2);
    }

    private void waste2_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste2_1.Visibility = Visibility.Hidden;
      BuyWaste(2);
    }

    private void waste2_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste2_2.Visibility = Visibility.Hidden;
      BuyWaste(2);
  
    }

    private void waste2_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste2_3.Visibility = Visibility.Hidden;
      BuyWaste(2);
  
    }

    private void coal3_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal3_1.Visibility = Visibility.Hidden;
      BuyCoal(3);
  
    }

    private void coal3_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal3_2.Visibility = Visibility.Hidden;
      BuyCoal(3);
    }

    private void coal3_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal3_3.Visibility = Visibility.Hidden;
      BuyCoal(3);
    }

    private void oil3_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil3_1.Visibility = Visibility.Hidden;
      BuyOil(3);
    }

    private void oil3_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil3_2.Visibility = Visibility.Hidden;
      BuyOil(3);
    }

    private void oil3_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil3_3.Visibility = Visibility.Hidden;
      BuyOil(3);
    }

    private void waste3_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste3_1.Visibility = Visibility.Hidden;
      BuyWaste(3);
    }

    private void waste3_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste3_2.Visibility = Visibility.Hidden;
      BuyWaste(3);
    }

    private void waste3_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste3_3.Visibility = Visibility.Hidden;
      BuyWaste(3);
    }

    private void coal4_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal4_1.Visibility = Visibility.Hidden;
      BuyCoal(4);
    }

    private void coal4_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal4_2.Visibility = Visibility.Hidden;
      BuyCoal(4);
    }

    private void coal4_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal4_3.Visibility = Visibility.Hidden;
      BuyCoal(4);
    }

    private void oil4_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil4_1.Visibility = Visibility.Hidden;
      BuyOil(4);
    }

    private void oil4_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil4_2.Visibility = Visibility.Hidden;
      BuyOil(4);
    }

    private void oil4_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil4_3.Visibility = Visibility.Hidden;
      BuyOil(4);
    }

    private void waste4_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste4_1.Visibility = Visibility.Hidden;
      BuyWaste(4);
    }

    private void waste4_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste4_2.Visibility = Visibility.Hidden;
      BuyWaste(4);
    }

    private void waste4_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste4_3.Visibility = Visibility.Hidden;
      BuyWaste(4);
    }

    private void coal5_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal5_1.Visibility = Visibility.Hidden;
      BuyCoal(5);
    }

    private void coal5_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal5_2.Visibility = Visibility.Hidden;
      BuyCoal(5);
    }

    private void coal5_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal5_3.Visibility = Visibility.Hidden;
      BuyCoal(5);
    }

    private void oil5_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil5_1.Visibility = Visibility.Hidden;
      BuyOil(5);
    }

    private void oil5_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil5_2.Visibility = Visibility.Hidden;
      BuyOil(5);
    }

    private void oil5_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil5_3.Visibility = Visibility.Hidden;
      BuyOil(5);
    }

    private void waste5_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste5_1.Visibility = Visibility.Hidden;
      BuyWaste(5);
    }

    private void waste5_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste5_2.Visibility = Visibility.Hidden;
      BuyWaste(5);
    }

    private void waste5_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste5_3.Visibility = Visibility.Hidden;
      BuyWaste(5);
    }

    private void coal6_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal6_1.Visibility = Visibility.Hidden;
      BuyCoal(6);
    }

    private void coal6_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal6_2.Visibility = Visibility.Hidden;
      BuyCoal(6);
    }

    private void coal6_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal6_3.Visibility = Visibility.Hidden;
      BuyCoal(6);
    }

    private void oil6_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil6_1.Visibility = Visibility.Hidden;
      BuyOil(6);
    }

    private void oil6_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil6_2.Visibility = Visibility.Hidden;
      BuyOil(6);
    }

    private void oil6_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil6_3.Visibility = Visibility.Hidden;
      BuyOil(6);
    }

    private void waste6_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste6_1.Visibility = Visibility.Hidden;
      BuyWaste(6);
    }

    private void waste6_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste6_2.Visibility = Visibility.Hidden;
      BuyWaste(6);
    }

    private void waste6_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste6_3.Visibility = Visibility.Hidden;
      BuyWaste(6);
    }

    private void coal7_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal7_1.Visibility = Visibility.Hidden;
      BuyCoal(7);
    }

    private void coal7_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal7_2.Visibility = Visibility.Hidden;
      BuyCoal(7);
    }

    private void coal7_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal7_3.Visibility = Visibility.Hidden;
      BuyCoal(7);
    }

    private void oil7_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil7_1.Visibility = Visibility.Hidden;
      BuyOil(7);
    }

    private void oil7_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil7_2.Visibility = Visibility.Hidden;
      BuyOil(7);
    }

    private void oil7_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil7_3.Visibility = Visibility.Hidden;
      BuyOil(7);
    }

    private void waste7_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste7_1.Visibility = Visibility.Hidden;
      BuyWaste(7);
    }

    private void waste7_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste7_2.Visibility = Visibility.Hidden;
      BuyWaste(7);
    }

    private void waste7_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste7_3.Visibility = Visibility.Hidden;
      BuyWaste(7);
    }

    private void coal8_1_Click(object sender, RoutedEventArgs e)
    {
      this.coal8_1.Visibility = Visibility.Hidden;
      BuyCoal(8);
    }

    private void coal8_2_Click(object sender, RoutedEventArgs e)
    {
      this.coal8_2.Visibility = Visibility.Hidden;
      BuyCoal(8);
    }

    private void coal8_3_Click(object sender, RoutedEventArgs e)
    {
      this.coal8_3.Visibility = Visibility.Hidden;
      BuyCoal(8);
    }

    private void oil8_1_Click(object sender, RoutedEventArgs e)
    {
      this.oil8_1.Visibility = Visibility.Hidden;
      BuyOil(8);
    }

    private void oil8_2_Click(object sender, RoutedEventArgs e)
    {
      this.oil8_2.Visibility = Visibility.Hidden;
      BuyOil(8);
    }

    private void oil8_3_Click(object sender, RoutedEventArgs e)
    {
      this.oil8_3.Visibility = Visibility.Hidden;
      BuyOil(8);
    }

    private void waste8_1_Click(object sender, RoutedEventArgs e)
    {
      this.waste8_1.Visibility = Visibility.Hidden;
      BuyWaste(8);
    }

    private void waste8_2_Click(object sender, RoutedEventArgs e)
    {
      this.waste8_2.Visibility = Visibility.Hidden;
      BuyWaste(8);
    }

    private void waste8_3_Click(object sender, RoutedEventArgs e)
    {
      this.waste8_3.Visibility = Visibility.Hidden;
      BuyWaste(8);
    }

    private void uranium1_Click(object sender, RoutedEventArgs e)
    {
      this.uranium1.Visibility = Visibility.Hidden;
      BuyUranium(1);
    }

    private void uranium2_Click(object sender, RoutedEventArgs e)
    {
      this.uranium2.Visibility = Visibility.Hidden;
      BuyUranium(2);
    }

    private void uranium3_Click(object sender, RoutedEventArgs e)
    {
      this.uranium3.Visibility = Visibility.Hidden;
      BuyUranium(3);
    }

    private void uranium4_Click(object sender, RoutedEventArgs e)
    {
      this.uranium4.Visibility = Visibility.Hidden;
      BuyUranium(4);
    }

    private void uranium5_Click(object sender, RoutedEventArgs e)
    {
      this.uranium5.Visibility = Visibility.Hidden;
      BuyUranium(5);
    }

    private void uranium6_Click(object sender, RoutedEventArgs e)
    {
      this.uranium6.Visibility = Visibility.Hidden;
      BuyUranium(6);
    }

    private void uranium7_Click(object sender, RoutedEventArgs e)
    {
      this.uranium7.Visibility = Visibility.Hidden;
      BuyUranium(7);
    }

    private void uranium8_Click(object sender, RoutedEventArgs e)
    {
      this.uranium8.Visibility = Visibility.Hidden;
      BuyUranium(8);
    }

    private void uranium10_Click(object sender, RoutedEventArgs e)
    {
      this.uranium10.Visibility = Visibility.Hidden;
      BuyUranium(10);
    }

    private void uranium12_Click(object sender, RoutedEventArgs e)
    {
      this.uranium12.Visibility = Visibility.Hidden;
      BuyUranium(12);
    }

    private void uranium14_Click(object sender, RoutedEventArgs e)
    {
      this.uranium14.Visibility = Visibility.Hidden;
      BuyUranium(14);
    }

    private void uranium16_Click(object sender, RoutedEventArgs e)
    {
      this.uranium16.Visibility = Visibility.Hidden;
      BuyUranium(16);
    }
    #endregion

    private void current_station1_MouseDown(object sender, MouseButtonEventArgs e)
    {
      if (newGame.Phase == 0)
      {
        newGame.CurrentStation = newGame.StationManager.StationsExpected.ElementAt(0).Value;
        newGame.AuctionManager.SelectedStationIndex = 1;
        DisplayAuction();
      }
    }
    private void current_station2_MouseDown(object sender, MouseButtonEventArgs e)
    {
      if (newGame.Phase == 0)
      {
        newGame.CurrentStation = newGame.StationManager.StationsExpected.ElementAt(1).Value;
        newGame.AuctionManager.SelectedStationIndex = 2;
        DisplayAuction();
      }
    }

    private void current_station3_MouseDown(object sender, MouseButtonEventArgs e)
    {
      if (newGame.Phase == 0)
      {
        newGame.CurrentStation = newGame.StationManager.StationsExpected.ElementAt(2).Value;
        newGame.AuctionManager.SelectedStationIndex = 3;
        DisplayAuction();
      }
    }

    private void current_station4_MouseDown(object sender, MouseButtonEventArgs e)
    {
      if (newGame.Phase == 0)
      {
        newGame.CurrentStation = newGame.StationManager.StationsExpected.ElementAt(3).Value;
        newGame.AuctionManager.SelectedStationIndex = 4;
        DisplayAuction();
      }
    }
    
    private void removeDialog_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
      newGame.BuyStationReplace(removeDialog.SelectedNum, removeDialog.BoughtNum);
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {

//        removeDialog.Close();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      
      newGame.UpdatePhase();
      Auction();

    }

    private void your_bet_textbox_TextChanged(object sender, TextChangedEventArgs e)
    {
      if ((your_bet_textbox.Text.ToString() == "0") || (your_bet_textbox.Text.ToString() == ""))
      {
        this.bet_button.IsEnabled = false;        
        return;
      }
      int your_bet = Convert.ToInt32(this.your_bet_textbox.Text.ToString());
      if (your_bet > newGame.AuctionManager.CurrentPlayer.Money)
      {
        your_bet_textbox.Text = newGame.AuctionManager.CurrentPlayer.Money.ToString();
        your_bet = Convert.ToInt32(this.your_bet_textbox.Text.ToString());
      }
      bool bet_condition;
      if (!this.fold_button.IsEnabled) // кнопка bet была нажата во время первого хода игрока, покупающего станцию
        bet_condition = (newGame.AuctionManager.HighestBet <= your_bet);// && (newGame.AuctionManager.YourBet <= newGame.AuctionManager.CurrentPlayer.Money);
      else // кнопка bet была нажата во время хода другого игрока
        bet_condition = (newGame.AuctionManager.HighestBet < your_bet);
      if (bet_condition)
      {
        this.bet_button.IsEnabled = true;
      }
      else
      {
        this.bet_button.IsEnabled = false;
      }
    }

    public void FindNextAuctionPlayer()
    {
      bool found = false;
      while (!found && newGame.AuctionManager.PlayersInAuction != 1)
      {
        newGame.AuctionManager.Pos = (newGame.AuctionManager.Pos + 1) % newGame.PlayerList.Count; // очередь другого чувака.
        if (newGame.PlayerList[newGame.AuctionManager.Pos].CanBet)
          if (newGame.PlayerList[newGame.AuctionManager.Pos].Money <= newGame.AuctionManager.HighestBet) // бабла не хватает
          {
            newGame.PlayerList[newGame.AuctionManager.Pos].CanBet = false;
            newGame.AuctionManager.PlayersInAuction--;
          }
          else
            found = true;
      }
      this.your_bet_label2.Content = newGame.PlayerList[newGame.AuctionManager.Pos].Name;
    }

    private void bet_button_Click(object sender, RoutedEventArgs e) 
    {
      if (!this.fold_button.IsEnabled) // кнопка bet была нажата во время первого хода игрока, покупающего станцию
      {
        this.fold_button.IsEnabled = true;
      }
      newGame.AuctionManager.YourBet = Convert.ToInt32(this.your_bet_textbox.Text.ToString()); // ставка, сделанная текущим игроком 
      newGame.AuctionManager.CurrentPlayer = newGame.PlayerList[newGame.AuctionManager.Pos];   // текущий игрок
      newGame.AuctionManager.HighestBet = Convert.ToInt32(this.your_bet_textbox.Text.ToString()); // Новая цена станции
      newGame.AuctionManager.HighestBetPlayer = newGame.PlayerList[newGame.AuctionManager.Pos];

      UpdateAuction();
      FindNextAuctionPlayer();
      if (newGame.AuctionManager.PlayersInAuction < 2)
      {
        TryBuyStation();
      }

    }

    void TryBuyStation()
    {
      newGame.PlayerList[newGame.AuctionManager.Pos].BuyStation = false;
      newGame.CurrentPlayer = newGame.AuctionManager.HighestBetPlayer;
      newGame.StationManager.StationsExpected.ElementAt(newGame.AuctionManager.SelectedStationIndex - 1).Value.Cost = newGame.AuctionManager.HighestBet;
      BuyStation(newGame.AuctionManager.SelectedStationIndex);
      MessageBox.Show("Player " + newGame.CurrentPlayer.Name + " bought this station! Gratz!");
      if (newGame.AuctionManager.PlayersInAuction == 1)
      {
        MessageBox.Show("Auction finished!!! Пора обновить интерфейс!");
        return;
      }
      InitAuction();
      if (newGame.AuctionManager.CurrentPlayer.StartedAuction)
      {
        while (true)
        {
          int i = ++newGame.AuctionManager.Turn;
          Player p = newGame.GetByRating(i);
          if (p.BuyStation) // Нашли игрока
            break;
        }
      }
      newGame.AuctionManager.CurrentPlayer.StartedAuction = false;
      Auction();
    }

    private void fold_button_Click(object sender, RoutedEventArgs e)
    {
      if (newGame.AuctionManager.PlayersInAuction == 2) // Остается один игрок, он и покупает
      {
        FindNextAuctionPlayer();
        newGame.AuctionManager.HighestBetPlayer = newGame.PlayerList[newGame.AuctionManager.Pos];
        TryBuyStation();
      }
      else
      {
        newGame.AuctionManager.PlayersInAuction--;
        newGame.PlayerList[newGame.AuctionManager.Pos].CanBet = false;
        FindNextAuctionPlayer();
      }
    }

  }
}







      //double panelSize = SystemParameters.VirtualScreenHeight - SystemParameters.FullPrimaryScreenHeight;
      //MessageBox.Show("VirtualScreen: " + SystemParameters.MaximizedPrimaryScreenHeight);
      //this.Width = SystemParameters.FullPrimaryScreenWidth;
      //this.Height = SystemParameters.MaximizedPrimaryScreenHeight;//(SystemParameters.FullPrimaryScreenWidth / 960.0) * 509.0;// -panelSize;
      //this.WindowState = WindowState.Maximized;
    
      //DisplayImg();
